package com.glory.erp.invoice.order.dao.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.glory.erp.invoice.order.dao.OrderDao;
import com.glory.erp.invoice.order.bean.Order;
import com.glory.erp.invoice.order.bean.OrderQuery;
import com.glory.erp.auth.base.BaseDaoImpl;
import com.glory.erp.auth.base.BaseQueryModel;

public class OrderDaoImpl extends BaseDaoImpl<Order> implements OrderDao {

	public void doQbc(DetachedCriteria dc, BaseQueryModel qm) {
		OrderQuery oqm = (OrderQuery) qm;
		if (oqm.getOrderType() != null && oqm.getOrderType() != -1) {
			dc.add(Restrictions.eq("orderType", oqm.getOrderType()));
		}
		if (oqm.getCreater() != null && oqm.getCreater().getName() != null
				&& oqm.getCreater().getName().trim().length() > 0) {
			dc.createAlias("creater", "c1");
			dc.add(Restrictions.like("c1.name", "%"
					+ oqm.getCreater().getName().trim() + "%"));
		}
		if (oqm.getChecker() != null && oqm.getChecker().getName() != null
				&& oqm.getChecker().getName().trim().length() > 0) {
			dc.createAlias("checker", "c2");
			dc.add(Restrictions.like("c2.name", "%"
					+ oqm.getChecker().getName().trim() + "%"));
		}
		if (oqm.getCompleter() != null && oqm.getCompleter().getName() != null
				&& oqm.getCompleter().getName().trim().length() > 0) {
			dc.createAlias("completer", "c3");
			dc.add(Restrictions.like("c3.name", "%"
					+ oqm.getCompleter().getName().trim() + "%"));
		}
		if (oqm.getCompleter() != null && oqm.getCompleter().getUuid() != null
				&& oqm.getCompleter().getUuid() != -1) {
			dc.add(Restrictions.eq("completer", oqm.getCompleter()));
		}
		dc.createAlias("supplier", "s");
		if (oqm.getSupplier() != null && oqm.getSupplier().getUuid() != null
				&& oqm.getSupplier().getUuid() != -1) {
			dc.add(Restrictions.eq("s.uuid", oqm.getSupplier()));
		}
		if (oqm.getSupplier() != null && oqm.getSupplier().getNeeds() != null
				&& oqm.getSupplier().getNeeds() != -1) {
			dc.add(Restrictions.eq("s.needs", oqm.getSupplier().getNeeds()));
		}
		if (oqm.getType() != null && oqm.getType() != -1) {
			dc.add(Restrictions.eq("type", oqm.getType()));
		}
		if (oqm.getTotalNumMin() != null && !oqm.getTotalNumMin().equals("")
				&& oqm.getTotalNumMax() != null
				&& !oqm.getTotalNumMax().equals("")) {
			dc.add(Restrictions.ge("totalNum", oqm.getTotalNumMin()));
			dc.add(Restrictions.le("totalNum", oqm.getTotalNumMax()));
		}
		if (oqm.getTotalPriceMin() != null
				&& !oqm.getTotalPriceMin().equals("")
				&& oqm.getTotalPriceMax() != null
				&& !oqm.getTotalPriceMax().equals("")) {
			dc.add(Restrictions.ge("totalPrice",
					Double.parseDouble(oqm.getTotalPriceMin())));
			dc.add(Restrictions.le("totalPrice",
					Double.parseDouble(oqm.getTotalPriceMax())));
		}
		if (oqm.getCreateTimeMin() != null && oqm.getCreateTimeMax() != null) {
			dc.add(Restrictions.ge("createTime", oqm.getCreateTimeMin()
					.getTime()));
			dc.add(Restrictions.le("createTime", oqm.getCreateTimeMax()
					.getTime()));
		}
	}

	private void doQbc2(DetachedCriteria dc, BaseQueryModel qm,
			Integer[] orderTypes) {
		dc.add(Restrictions.in("orderType", orderTypes));
		doQbc(dc, qm);
	}

	private void doQbc3(DetachedCriteria dc, BaseQueryModel qm,
			Integer[] orderTypes) {
		dc.add(Restrictions.in("type", orderTypes));
		doQbc(dc, qm);
	}

	public List<Order> findAllOrderTypes(OrderQuery orderQuery,
			Integer pageNum, Integer pageCount, Integer[] orderTypes) {
		DetachedCriteria dc = DetachedCriteria.forClass(Order.class);
		doQbc2(dc, orderQuery, orderTypes);
		return this.getHibernateTemplate().findByCriteria(dc,
				(pageNum - 1) * pageCount, pageCount);
	}

	public Integer getCountOrderTypes(OrderQuery orderQuery,
			Integer[] orderTypes) {
		DetachedCriteria dc = DetachedCriteria.forClass(Order.class);
		dc.setProjection(Projections.rowCount());
		doQbc2(dc, orderQuery, orderTypes);
		List<Long> count = this.getHibernateTemplate().findByCriteria(dc);
		return count.get(0).intValue();
	}

	public List<Order> findAllTypes(OrderQuery orderQuery, Integer pageNum,
			Integer pageCount, Integer[] types) {
		DetachedCriteria dc = DetachedCriteria.forClass(Order.class);
		doQbc3(dc, orderQuery, types);
		return this.getHibernateTemplate().findByCriteria(dc,
				(pageNum - 1) * pageCount, pageCount);
	}

	public Integer findAllTypes(OrderQuery orderQuery, Integer[] types) {
		DetachedCriteria dc = DetachedCriteria.forClass(Order.class);
		dc.setProjection(Projections.rowCount());
		doQbc3(dc, orderQuery, types);
		List<Long> count = this.getHibernateTemplate().findByCriteria(dc);
		return count.get(0).intValue();
	}
}